package com.songyuquan.webcs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songyuquan.webcs.mapper.ProductCategoryMapper;
import com.songyuquan.webcs.mapper.ProductMapper;
import com.songyuquan.webcs.pojo.Product;
import com.songyuquan.webcs.pojo.ProductCategory;
import com.songyuquan.webcs.service.ProductCategoryService;
import com.songyuquan.webcs.vo.ProductCategoryVO;
import com.songyuquan.webcs.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements ProductCategoryService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<ProductCategoryVO> getAllProductCategoryVO() {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("type", 1);
        List<ProductCategory> levelOne = productCategoryMapper.selectList(wrapper);
        List<ProductCategoryVO> levelOneVO = levelOne.stream().map(e -> new ProductCategoryVO(e.getId(), e.getName())).collect(Collectors.toList());
        //top图片
        for (int i = 0; i < levelOneVO.size(); i++) {
            levelOneVO.get(i).setTopImg("/images/top" + i + ".png");
            wrapper = new QueryWrapper();
            wrapper.eq("categorylevelone_id", levelOneVO.get(i).getId());
            List<Product> productList = productMapper.selectList(wrapper);
            List<ProductVO> productVOList = productList.stream().map(e -> new ProductVO(
                    e.getId(),
                    e.getName(),
                    e.getPrice(),
                    e.getFileName()
            )).collect(Collectors.toList());
            levelOneVO.get(i).setProductVOList(productVOList);
        }
        return levelOneVO;
    }
}
